/*
 * Copyright 2008-2009 Aying.Net Powered by Fuchun.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.aying.util;

import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

import net.aying.util.Utils;

/**
 * {@code Utils} 类的测试用例。
 * 
 * @author Fuchun
 * @version 1.0, 2009-6-17
 */
public class UtilsTestCase extends UtilBasedTestCase {

    private static final Log log = LogFactory.getLog(UtilsTestCase.class);

    private static final String[] ALPHABET = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

    protected StringBuffer testString, testNullString;
    protected Set<String> testSet, testNullSet;
    protected Iterator<String> testIterator, testNullIterator;
    protected Map<String, String> testMap, testNullMap;
    protected List<String> testList, testNullList;
    protected String[] testArray, testNullArray;

    public UtilsTestCase(String testName) {
        super(testName);
    }

    private void initTestvar() {
        testSet = GenericUtils.getHashSet(Arrays.asList(ALPHABET));
        testMap = GenericUtils.getMap();
        testString = new StringBuffer();
        for (String letter : ALPHABET) {
            testMap.put(letter, letter);
            testString.append(letter);
        }
        testList = GenericUtils.getList(Arrays.asList(ALPHABET));
        testIterator = testList.iterator();
        testArray = ALPHABET;
        
        testNullString = new StringBuffer();
        testNullSet = GenericUtils.getHashSet();
        testNullMap = GenericUtils.getMap();
        testNullList = GenericUtils.getList();
        testNullIterator = testNullList.iterator();
        testNullArray = new String[] {};
    }

    /**
     * @see junit.framework.TestCase#setUp()
     */
    @Override
    public void setUp() throws Exception {
        long start = System.currentTimeMillis();
        initTestvar();
        log.info("setUp method run time is " + (System.currentTimeMillis() - start) + " ms.");
    }

    /**
     * @see junit.framework.TestCase#tearDown()
     */
    @Override
    public void tearDown() throws Exception {
        testIterator = null;
        testSet = null;
        testMap = null;
        testList = null;
        testString = null;
    }

    public void test_getLength() throws Exception {
        long start = System.currentTimeMillis();
        assertEquals(ALPHABET.length, Utils.getLength(testString).intValue());
        assertEquals(ALPHABET.length, Utils.getLength(testString.toString()).intValue());
        assertEquals(ALPHABET.length, Utils.getLength(testSet).intValue());
        assertEquals(ALPHABET.length, Utils.getLength(testIterator).intValue());
        assertEquals(ALPHABET.length, Utils.getLength(testMap).intValue());
        assertEquals(ALPHABET.length, Utils.getLength(testList).intValue());

        assertEquals(0, Utils.getLength(testNullString).intValue());
        assertEquals(0, Utils.getLength(testNullSet).intValue());
        assertEquals(0, Utils.getLength(testNullIterator).intValue());
        assertEquals(0, Utils.getLength(testNullMap).intValue());
        assertEquals(0, Utils.getLength(testNullList).intValue());

        assertEquals(2, Utils.getLength(90L).intValue());
        log.info("getLength Method run time is " + (System.currentTimeMillis() - start) + " ms.");
    }

    public void test_isEmpty() throws Exception {
        long start = System.currentTimeMillis();
        assertFalse(Utils.isEmpty(testString));
        assertFalse(Utils.isEmpty(testString.toString()));
        assertFalse(Utils.isEmpty(testSet));
        assertFalse(Utils.isEmpty(testIterator));
        assertFalse(Utils.isEmpty(testMap));
        assertFalse(Utils.isEmpty(testList));
        assertFalse("Utils.isEmpty(testArray) is true", Utils.isEmpty(testArray));
        
        assertTrue(Utils.isEmpty(testNullString));
        assertTrue(Utils.isEmpty(testNullSet));
        assertTrue(Utils.isEmpty(testNullIterator));
        assertTrue(Utils.isEmpty(testNullMap));
        assertTrue(Utils.isEmpty(testNullList));
        assertTrue(Utils.isEmpty(testNullArray));
        log.info("isEmpty Method run time is " + (System.currentTimeMillis() - start) + " ms.");
    }

    public static Test suite() {
        return new TestSuite(UtilsTestCase.class);
    }

    public static void main(String[] args) throws Exception {
        TestRunner.run(suite());
    }
}
